i = 0
rf.results <- list()
for (year in start.year:end.year) {
  i = i + 1
  print(year)
  # Fit Random Forest
  rf.results[[i]] <- list()
  if (is.element(rhs.group,c("full",
                                 "noviolence",
                                 "onlyviolence",
                                 "lagdv",
                                 "violpop"))) { # Only get the optimal discrimination threshold for main predictor sets.
  dta.past <- dta[dta[,t.var]< year,]
  N <- length(dta.past[,dv])
  cv_results <- foreach (cv=1:cv.runs, 
                         .combine='rbind',
                         .packages = c("randomForest",
                                       "pROC")) %dopar% {   # Loop over CV runs
           set.seed(52*cv) 
           shuffle <- sample(1:N,
                             N,
                             replace=FALSE)
           pred.prob <- rep(NA,N)
           sampleSplit = c(0,round((N/5*c(1:5))))
           for (f in 1:5) {
             # Fit lasso
             cv.fit = randomForest(x = as.matrix(dta.past[-shuffle[(sampleSplit[f]+1):(sampleSplit[f+1])],
                                                               rhs]),
                                   y = as.matrix(dta.past[-shuffle[(sampleSplit[f]+1):(sampleSplit[f+1])],
                                                               dv]),
                                   ntree = rf_params$ntree,
                                   maxnodes = rf_params$maxnodes,
                                   type = rf_params$type,
                                   importance = rf_params$importance
             )
             pred.prob[shuffle[(sampleSplit[f]+1):(sampleSplit[f+1])]
                       ] <- predict(cv.fit,
                                    newdata = as.matrix(dta.past[shuffle[(sampleSplit[f]+1):(sampleSplit[f+1])],
                                                                      rhs]))
           }
           cv_results <- c("CV Run"=0,
                           "DT"=as.numeric(mostaccDT(y=dta.past[,dv],
                                                     yhat=pred.prob,
                                                     J=1000)),
                           "DTsens"=as.numeric(bestDT(y=dta.past[,dv],
                                                     yhat=pred.prob,
                                                     J=1000)))
           return(cv_results)
                                       }
    if (cv.runs>1) {
      rf.results[[i]]$dt <- mean(cv_results[,"DT"])
      rf.results[[i]]$dtsens <- mean(cv_results[,"DTsens"])  
    } else {
      rf.results[[i]]$dt <- cv_results["DT"]
      rf.results[[i]]$dtsens <- cv_results["DTsens"] 
    }
    
  }
  hours <- floor((proc.time()[3]-start.time)/3600)
  mins <- floor((proc.time()[3]-start.time)/60) - hours*60
  secs <- floor((proc.time()[3]-start.time)) - hours*3600 - mins*60
  
  print(paste(hours,
              "h",
              mins,
              "m",
              secs,
              "s elapsed",
              sep = ""))
  set.seed(i)
  rf.results[[i]]$mod <-  randomForest(x = as.matrix(dta[dta[,t.var] < year,
                                                              rhs]),
                           y = as.matrix(dta[dta[,t.var] < year,
                                                  dv]),
                           ntree = rf_params$ntree,
                           maxnodes = rf_params$maxnodes,
                           type = rf_params$type,
                           importance = rf_params$importance
  )
  rf.results[[i]]$fit.oos <- predict(rf.results[[i]]$mod,
                                       newdata = as.matrix(dta[dta[,t.var] == year,
                                                                    rhs]))
  rf.results[[i]]$actual.oos <- as.matrix(dta[dta[,t.var] == year,
                                                      dv])
  print(year)
}
save(rf.results,file=paste(modeldir,"/",
                           country,
                           "_rf_",
                           v,
                           "_",
                           rhs.group,
                           fileext,
                           ".RData",
                           sep = ""))